package com.bw.mvvm_common.mvvm.view;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import com.bw.mvvm_common.BR;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

public abstract class BaseActivity <V extends ViewDataBinding,VM extends BaseViewModel>extends
        AppCompatActivity implements IView {

    protected V v;
    protected VM vm;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewModel();
        getLifecycle().addObserver(vm);
        vm.getUiChange().getHideDialogObservable().observe(this,  hideDialogObserver);
        vm.getUiChange().getFinishActObservable().observe(this,finishObserver);
        vm.getUiChange().getShowDialogObservable().observe(this,showDialogObserver);
        vm.getUiChange().getShowMsgObservable().observe(this,showMsgObserver);
        vm.getUiChange().getStartPageObservable().observe(this,startPageObserver);
        bindDataBinding();
        v.setVariable(initVerable(),vm);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        vm.getUiChange().getHideDialogObservable().removeObserver(hideDialogObserver);
        vm.getUiChange().getFinishActObservable().removeObserver(finishObserver);
        vm.getUiChange().getShowDialogObservable().removeObserver(showDialogObserver);
        vm.getUiChange().getShowMsgObservable().removeObserver(showMsgObserver);
        vm.getUiChange().getStartPageObservable().removeObserver(startPageObserver);
        getLifecycle().removeObserver(vm);
        v.unbind();
    }

    @Override
    public void initViewModel() {
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        Type[]types = parameterizedType.getActualTypeArguments();
        Class clazz = (Class) types[1];
        vm = (VM)new ViewModelProvider(this,
                new ViewModelProvider.NewInstanceFactory()).get(clazz);
    }



    @Override
    public void bindDataBinding() {
       v =  DataBindingUtil.setContentView(this,bindLayout());
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void finishPage() {
         finish();
    }

    @Override
    public void startPage(Class clazz, Bundle bundle) {
        Intent intent = new Intent(this,clazz);
        if (bundle!=null){
            intent.putExtra(UIField.PAGEBUNDLE,bundle);
        }
        startActivity(intent);
    }

    private Observer<String>showDialogObserver = new Observer<String>(){
        @Override
        public void onChanged( String o) {
            showDialog();
        }
    };
    private Observer<String>hideDialogObserver = new Observer<String>(){
        @Override
        public void onChanged( String o) {
            hideDialog();
        }
    };
    private Observer<String>finishObserver = new Observer<String>(){
        @Override
        public void onChanged( String o) {
            finishPage();
        }
    };
    private Observer<String>showMsgObserver = new Observer<String>(){
        @Override
        public void onChanged( String o) {
           showMsg(o);
        }
    };

    private Observer<HashMap<String,Object>>startPageObserver = new Observer<HashMap<String, Object>>() {
        @Override
        public void onChanged( HashMap<String, Object> map) {
            Class clazz = (Class) map.get(UIField.PAGECLAZZ);
            Object o = map.get(UIField.PAGEBUNDLE);
            Bundle bundle = null;
            if (o!=null)
                bundle = (Bundle) o;
            startPage(clazz,bundle);
        }
    };

}
